package com.menu.Service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.menu.Mapper.MessageMapper;
import com.menu.Mapper.UserMapper;
import com.menu.Pojo.Message;
import com.menu.Pojo.MessageDetail;
import com.menu.Pojo.MessageVo;
import com.menu.Pojo.User;
import com.menu.Service.MessageService;
import com.menu.Utils.FileUtils;
import com.menu.Utils.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class MessageServiceImpl implements MessageService {

    @Autowired
    private FileUtils fileUtils;
    @Resource
    private MessageMapper messageMapper;
    @Resource
    private UserMapper userMapper;

    @Override
    public ResponseResult sendMessage(Integer userId,String question, Integer type, Long extendId, MultipartFile[] multipartFile) {
        ResponseResult responseResult = null;
        if (multipartFile != null && multipartFile.length != 0){
           responseResult = fileUtils.uploadMessage(userId, multipartFile);
        }
        if (responseResult != null && responseResult.getCode() != 200){
            return responseResult;
        }
        StringBuilder stringBuilder = new StringBuilder();
        if (responseResult != null && responseResult.getCode() == 200){
            Object object = responseResult.getObject();
            String []path = (String[])object;
            for (String temp:path){
                stringBuilder.append(temp);
                stringBuilder.append(":");
            }
            stringBuilder.deleteCharAt(stringBuilder.lastIndexOf(":"));
        }
        Message message = new Message();
        message.setExtendId(extendId);
        message.setType(type);
        Date date=new Date();
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date1=simpleDateFormat.format(date);
        message.setCreatTime(date1);
        message.setQuestion(question);
        message.setAttachment(stringBuilder.toString());
        message.setExtendId(extendId);
        message.setUserId(userId);
        Integer integer = messageMapper.sendMessage(message);
        if (integer == 1){
            return ResponseResult.success("反馈成功！");
        }
        return ResponseResult.error("服务器繁忙！");
    }

    @Override
    public ResponseResult queryMessage(Integer userId,Integer type ,Integer state, Integer pageStart, Integer pageSize) {
        PageHelper.startPage(pageStart,pageSize);
        List<Message> messages = messageMapper.queryMessage(userId,type,state);
        List<MessageDetail> list = new ArrayList<>();
        for (Message message:
             messages) {
            MessageDetail messageDetail = new MessageDetail();
            BeanUtils.copyProperties(message,messageDetail);
            User user = userMapper.selectDetails(message.getUserId());
            messageDetail.setUserHeadImg(user.getHeadImg());
            messageDetail.setUserNickname(user.getNickname());
            User admin = null;
            if (message.getReplyId() != null){
                admin = userMapper.selectDetails(message.getReplyId());
                messageDetail.setAdminHeadImg(admin.getHeadImg());
                messageDetail.setAdminNickname(admin.getNickname());
            }
            list.add(messageDetail);
        }
        PageInfo<MessageVo> pageInfo = new PageInfo(messages);
        PageInfo<MessageVo> pageInfos = new PageInfo(list);
        pageInfos.setTotal(pageInfo.getTotal());
        return ResponseResult.success(pageInfos);
    }

    @Override
    public ResponseResult AqueryMessage( Integer type, Integer state) {
        List<MessageVo> messageVos = messageMapper.queryforVo(type, state);
        return ResponseResult.success(messageVos);
    }

    @Override
    public ResponseResult queryForDetail(Long messageId) {
        Message message = messageMapper.queryForDetail(messageId);
        if (message == null){
            return ResponseResult.error("查询不存在！");
        }
        MessageDetail messageDetail = new MessageDetail();
        BeanUtils.copyProperties(message,messageDetail);
        User user = userMapper.selectDetails(message.getUserId());
        messageDetail.setUserHeadImg(user.getHeadImg());
        messageDetail.setUserNickname(user.getNickname());
        User admin = null;
        if (message.getReplyId() != null){
            admin = userMapper.selectDetails(message.getReplyId());
            messageDetail.setAdminHeadImg(admin.getHeadImg());
            messageDetail.setAdminNickname(admin.getNickname());
        }
        return ResponseResult.success(messageDetail);
    }

    @Override
    public ResponseResult queryForDetail(Long messageId, Integer userId) {
        Message message = messageMapper.queryForDetail(messageId);
        if (message == null || !message.getUserId().equals(userId)){
            return ResponseResult.error("查询不存在！");
        }
        MessageDetail messageDetail = new MessageDetail();
        if (message.getState() == 1){
            messageMapper.updateState(new Long[]{messageId});
        }
        BeanUtils.copyProperties(message,messageDetail);
        User user = userMapper.selectDetails(message.getUserId());
        messageDetail.setUserHeadImg(user.getHeadImg());
        messageDetail.setUserNickname(user.getNickname());
        User admin = null;
        if (message.getReplyId() != null){
            admin = userMapper.selectDetails(message.getReplyId());
            messageDetail.setAdminHeadImg(admin.getHeadImg());
            messageDetail.setAdminNickname(admin.getNickname());
        }
        return ResponseResult.success(messageDetail);
    }

    @Override
    public ResponseResult resolve(Integer userId, String content, Long messageId) {
        Date date=new Date();
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date1=simpleDateFormat.format(date);
        if (messageMapper.resolve(userId,content,messageId,date1) == 1){
            return ResponseResult.success("处理成功！");
        }
        return ResponseResult.error("服务器繁忙！");
    }

    @Override
    public ResponseResult deleteMessage(Integer[] messageId, Integer userId) {
        Integer integer = messageMapper.deleteMessage(messageId, userId);
        if (integer == 0){
            return ResponseResult.error("删除失败！");
        }
        return ResponseResult.success("删除成功!");
    }

    @Override
    public ResponseResult queryForMessage(Integer userId) {
        Integer integer = messageMapper.queryForMessage(userId);
        if (integer == null){
            return ResponseResult.success(0);
        }
        return ResponseResult.success(integer);
    }
}
